<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins" @checkboxRecords="checkboxRecords">
      <template #form="{ form }">
        <el-input
          v-model.trim="form.name"
          class="!w-[160px]"
          placeholder="品牌名称"
          clearable
        ></el-input>
        <el-input
          v-model.trim="form.alias"
          class="!w-[160px]"
          placeholder="品牌中文名"
          clearable
        ></el-input>
        <el-select
          v-model="form.type"
          class="!w-[160px]"
          placeholder="品牌类型"
          clearable
        >
          <el-option
            v-for="item in options.typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select v-model="form.status" placeholder="状态" class="!w-[160px]" clearable>
          <el-option
            v-for="item in options.statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </template>
      <template #toolbar_btns>
        <VButton auth="/goods/basics/brand/add" type="primary" @click="open()"
          ><svg-icon icon-class="add_new" />新增</VButton
        >
        <VButton
          auth="/goods/basics/brand/enable"
          :loading="loadings.enable"
          @click="handleStatus(1)"
        >
          <svg-icon class="mr-1 base-text-color" icon-class="state_finish_enable" />
          启用</VButton
        >
        <VButton
          auth="/goods/basics/brand/disable"
          :loading="loadings.disable"
          @click="handleStatus(2)"
        >
          <svg-icon
            class="mr-1 text-red-500"
            icon-class="state_finish_cover"
          />禁用</VButton
        >
        <VButton auth="/goods/basics/brand/delete" :loading="loadings.del" @click="del"
          ><svg-icon class="mr-1 text-red-500" icon-class="del" />删除</VButton
        >
      </template>
    </VTable>
    <ComModal ref="addref" @query="query" :options="options"></ComModal>
    <VLog ref="logRef" class="dialogPublic" :api="api.getBrandsLog" options="id"></VLog>
  </VPage>
</template>

<script setup lang="jsx" name="BasicsBrand">
import { ElMessage, ElMessageBox } from "element-plus";
import VImage from "@/components/Image/index.vue";
import ComModal from "./components/ComModal.vue";
import { getOption } from "@/api/index.js";
import VLog from "@/components/VLog";
import api from "./api";
import { toRefs } from "vue";
const tableRef = ref();
const addref = ref(null);
const records = ref([]);

const loadings = reactive({
  enable: false,
  disable: false,
  del: false,
});

const tableOptins = reactive({
  formConfig: {
    data: {},
  },
  rowConfig: {
    height: 52,
  },
  columns: [
    { type: "checkbox", width: 50 },
    { field: "id", title: "ID", width: 100 },
    {
      field: "name",
      title: "品牌名",
      minWidth: 100,
      slots: {
        default: ({ row }) => {
          return <div>{row.name}</div>;
        },
      },
    },
    { field: "alias", title: "品牌中文名", minWidth: 100 },
    {
      field: "img",
      title: "logo",
      width: 96,
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex items-center">
              <VImage src={row.img} width={40} height={40} />
            </div>
          );
        },
      },
    },
    { field: "type_cn", title: "品牌类型", width: 100 },
    { field: "customs_type_text", title: "报关品牌类型", minWidth: 120 },
    {
      field: "status_cn",
      title: "状态",
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex items-center">
              {row.status == 1 ? (
                <svg-icon class="mr-1 base-text-color" icon-class="state_finish_enable" />
              ) : (
                <svg-icon class="mr-1 text-red-500" icon-class="state_finish_cover" />
              )}
              {row.status_cn}
            </div>
          );
        },
      },
      width: 95,
    },
    {
      field: "desc",
      title: "备注",
      showOverflow: "title",
      slots: {
        default: ({ row }) => {
          return <div class="overflow-ellipsis overflow-hidden">{row.desc}</div>;
        },
      },
      width: 160,
    },
    {
      field: "create_user",
      title: "添加人",
      width: 200,
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex flex-col justify-start">
              <div>{row.create_user}</div>
              <div class="text-gray-400">{row.create_time}</div>
            </div>
          );
        },
      },
    },
    {
      field: "update_user",
      title: "修改人",
      width: 200,
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex flex-col">
              <div>{row.update_user}</div>
              <div class="text-gray-400">{row.update_time}</div>
            </div>
          );
        },
      },
    },

    {
      title: "操作",
      fixed: "right",
      width: 120,
      slots: {
        default: ({ row }) => {
          return (
            <div class="flex justify-center">
              {row.status == 1 || row.status == 2 ? (
                <VButton
                  auth="/goods/basics/brand/edit"
                  class="mr-4"
                  type="text"
                  underline={false}
                  onClick={() => open(row.id)}
                >
                  编辑
                </VButton>
              ) : (
                ""
              )}
              <el-link type="primary" underline={false} onClick={() => handleLog(row.id)}>
                日志
              </el-link>
            </div>
          );
        },
      },
    },
  ],

  proxyConfig: {
    props: {
      result: "data",
      total: "total",
    },
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page }),
    },
  },
});
// 列表数据
const findPageList = (data) => {
  return new Promise((resolve) => {
    api
      .query(data)
      .then((res) => {
        resolve({
          result: res.data,
          total: res.total,
        });
      })
      .catch(() => resolve([]));
  });
};
onMounted(() => {
  fetchOption();
});
const query = () => {
  tableRef?.value.query();
};

const data = reactive({
  options: {
    typeList: [],
  },
});
const { options } = toRefs(data);

const fetchOption = () => {
  // option不同接口判断
  getOption("brand").then((res) => {
    options.value = res.data;
  });
};
// 选中
const checkboxRecords = (arr) => {
  records.value = arr;
};

//获取已选中的行
const getSelectRow = () => {
  return tableRef?.value.$table.getCheckboxRecords();
};

const logRef = ref();
function handleLog(id) {
  logRef.value.open(id);
}

const open = (id) => {
  nextTick(() => {
    addref.value.open(id);
  });
};
const isHandle = () => {
  const records = getSelectRow();
  return !records.length ? ElMessage.error("请先选择行!") && false : true;
};
const handleStatus = (status) => {
  const records = getSelectRow().map((i) => i.id);
  if (!isHandle()) return;
  status == 1 ? (loadings.enable = true) : (loadings.disable = true);
  api
    .updateStatus({ id: records, status })
    .then((res) => {
      query();
      records.value = [];
      ElMessage.success(res.message);
    })
    .finally(() => {
      status == 1 ? (loadings.enable = false) : (loadings.disable = false);
    });
};
const del = () => {
  if (!isHandle()) return;
  const records = getSelectRow().map((i) => i.id);
  ElMessageBox.confirm("此操作将永久删除该项, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "btnFalses",
  }).then(() => {
    loadings.del = true;
    api
      .del({ id: records })
      .then((res) => {
        query();
        // records.value = []
        ElMessage.success(res.message);
      })
      .finally(() => {
        loadings.del = false;
      });
  });
};
</script>
